﻿using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[AddComponentMenu("BToolkit/SwitchButton")]
[RequireComponent(typeof(Image))]
public class SwitchButton : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
{
    public static event Action PlayGlobalButtonSoundAction;

    public int index;
    public GameObject off, on;
    public SwitchButtonGroup buttonGroup;
    public float pressScale = 0.98f;
    public bool globalSound = true;
    public RectTransform rectTransform { get { return transform as RectTransform; } }
    [Serializable]
    public class BoolEvent : UnityEvent<bool> { }
    [Space]
    public BoolEvent onStateChanged;
    Vector3 defaultScale;
    int currPointerId;

    public bool isOn
    {
        get { return on.activeSelf; }
        private set
        {
            on?.SetActive(value);
            off?.SetActive(!value);
        }
    }

    void OnDestroy()
    {
        if (buttonGroup) buttonGroup._OnRemoveButton(this);
        SwitchButtonGroup.onGroupInit -= OnGroupInit;
    }

    void Awake()
    {
        if (buttonGroup) buttonGroup._OnAddButton(this);
        SwitchButtonGroup.onGroupInit += OnGroupInit;
    }

    void Start() { }

    void OnGroupInit(SwitchButtonGroup buttonGroup)
    {
        if (this.buttonGroup == buttonGroup)
        {
            buttonGroup._OnAddButton(this);
        }
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (!buttonGroup)
        {
            //没有buttonGroup用Click
            SetIsOn(!isOn, true);
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if (enabled)
        {
            if (buttonGroup)
            {
                //有buttonGroup，用Down
                SetIsOn(!isOn, true);
            }

            if (pressScale != 1f)
            {
                if (defaultScale == Vector3.zero)
                {
                    defaultScale = transform.localScale;
                }
                transform.localScale = defaultScale * pressScale;
            }
            currPointerId = eventData.pointerId;
        }
    }

    public void SetIsOn(bool b, bool triggerEvent)
    {
        isOn = b;
        //事件
        if (triggerEvent)
        {
            onStateChanged?.Invoke(isOn);
            if (isOn)
            {
                buttonGroup?._OnSwitchOn(index);
            }
        }
        //声音
        if (buttonGroup)
        {
            if (isOn)
            {
                if (globalSound) PlayGlobalButtonSoundAction?.Invoke();
            }
        }
        else
        {
            if (globalSound) PlayGlobalButtonSoundAction?.Invoke();
        }
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        bool canExecUp = true;
        if (Application.isMobilePlatform)
        {
            canExecUp = (currPointerId == eventData.pointerId);
        }
        if (canExecUp)
        {
            currPointerId = -1;
            //变换效果恢复
            if (defaultScale != Vector3.zero) transform.localScale = defaultScale;
        }
    }

#if UNITY_EDITOR
    [UnityEditor.MenuItem("BToolkit/创建 SwitchButton")]
    [UnityEditor.MenuItem("GameObject/BToolkit/创建 SwitchButton")]
    static void Create()
    {
        GameObject go = new GameObject("SwitchButton");
        go.layer = LayerMask.NameToLayer("UI");
        go.transform.SetParent(UnityEditor.Selection.activeGameObject.transform, false);
        SwitchButton button = go.AddComponent<SwitchButton>();
        button.off = new GameObject("Off", typeof(RectTransform));
        button.off.transform.SetParent(go.transform, false);
        button.on = new GameObject("On", typeof(RectTransform));
        button.on.transform.SetParent(go.transform, false);
        UnityEditor.EditorUtility.SetDirty(go);
    }
#endif
}